import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';

enum Api {
  list = '/system/role/page',
  save = '/system/role/create',
  edit = '/system/role/update',
  setRoleStatus = '/system/role/updateStatus',
  deleteRole = '/system/role/delete',
  deleteBatch = '/system/role/deleteBatch',
  exportXls = '/system/role/exportXls',
  isRoleExist = '/system/role/checkRoleCode',
  queryTreeListForRole = '/system/role',
  queryRolePermission = '/system/permission',
  saveRolePermission = '/system/role/',
}

/**
 * 列表接口
 * @param params
 */
export const pageList = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除角色
 */
export const deleteRole = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteRole, params }).then(() => {
    handleSuccess();
  });
};
/**
 * 批量删除角色
 * @param params
 */
export const batchDeleteRole = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp
        .delete(
          {
            url: Api.deleteBatch,
            data: params,
          },
          { joinParamsToUrl: true },
        )
        .then(() => {
          handleSuccess();
        });
    },
  });
};
/**
 * 保存或者更新角色
 * @param params
 */
export const saveOrUpdateRole = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

export const setRoleStatus = (id: number, status: number) =>
  defHttp.post({ url: Api.setRoleStatus + `/${id}`, params: { id, status } });

/**
 * 编码校验
 * @param params
 */
export const isRoleExist = (params) =>
  defHttp.get({ url: Api.isRoleExist, params }, { isTransformResponse: false });

/**
 * 根据角色查询树信息
 */
export const queryTreeListForRole = (id) =>
  defHttp.get({ url: Api.queryTreeListForRole + `/${id}/menus` });
/**
 * 查询角色权限
 */
export const queryRolePermission = (params) =>
  // defHttp.get({ url: Api.queryRolePermission, params });
  defHttp.get({ url: Api.queryTreeListForRole + `/${params.roleId}/menus` });
/**
 * 保存角色权限
 */
export const saveRolePermission = (params) =>
  defHttp.post({ url: Api.saveRolePermission + `/${params.roleId}/menus`, params });
